
package com.monlyu.action.user;

import java.util.List;

import com.google.common.collect.Lists;
import com.monlyu.action.BaseAction;
import com.monlyu.bo.RoleBo;
import com.monlyu.bo.UserBo;
import com.monlyu.vo.Role;
import com.monlyu.vo.User;
import com.monlyu.vo.User2Role;

/********************************************************************************
 * <b>功能描述☞</b>编辑系统用户的信息<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>monlyu<br>
 * <b>文件路径☞</b>com.monlyu.action.user.EditUserAction.java<br>
 * <b>开发日期☞</b>2010-1-14 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.5
 ********************************************************************************/
public class EditUserAction extends BaseAction {

    private User user;
    private UserBo userBo;
    private RoleBo roleBo;
    private String rolelist;
    private List<User2Role> SELF_ROLE = Lists.newArrayList();
    private Long[] myRole;
    private String oripwd;
    private boolean status;

    public boolean isStatus(){
        return status;
    }

    public String view(){
        status = true;
        if (user != null && user.getId() != null) {
            userBo.load(user);
            SELF_ROLE = roleBo.getRoleByUid(user.getId());
        }
        rolelist = build();
        return view("editUser.jsp");
    }

    public String assignt(){
        status = true;
        if (user != null && user.getId() != null) {
            userBo.load(user);
            SELF_ROLE = roleBo.getRoleByUid(user.getId());
        }
        rolelist = build();
        return view("assigntUser.jsp");
    }

    /**
     * 查看自己的信息.并可用修改
     */
    public String self(){
    	status = false;
        rolelist = getRoleInfo();
        user = userBo.load();
        return view("editUser.jsp");
    }

    public String setFloder(){
        user = userBo.load();
        return view("setFloder.jsp");
    }

    // 保存floder
    public String saveFloder(){
        String f = user.getFolder();
        String fb = user.getFolderbak();
        user = userBo.load();
        user.setFolder(f);
        user.setFolderbak(fb);
        userBo.save(user);
        return json("ok");
    }

    /**
     * 组装信息
     */
    private String getRoleInfo(){
        List<User2Role> u2rs = roleBo.getRole();
        StringBuilder builder = new StringBuilder();
        for (User2Role user2Role : u2rs) {
            Role role = roleBo.get(Role.class, user2Role.getRoleId());
            builder.append(role.getName());
            builder.append("|<input name='myRole' type='hidden' value='" + user2Role.getRoleId() + "' />");
        }
        return builder.toString();
    }

    /**
     * 密码重置
     */
    public String repwd(){
        return ajax(userBo.repwd(user, oripwd, false));
    }

    /**
     * 强制重置密码
     */
    public String focuspwd(){
        return ajax(userBo.repwd(user, oripwd, true));
    }

    /**
     * 组装选择框字符串
     */
    private String build(){
        StringBuilder builder = new StringBuilder("");
        List<Role> allRoles = roleBo.getRollList();
        for (Role role : allRoles) {
            if (!role.isEnable()) {
                continue;
            }
            builder.append("<input type=\"checkbox\" name=\"myRole\" value=\"");
            builder.append(role.getId());
            builder.append("\" ");
            builder.append(check(role.getId()));
            builder.append(" />");
            builder.append(role.getName());
        }
        return builder.toString();
    }

    /**
     * 检查是否为选中状态
     */
    private String check(Long roledid){
        if (!SELF_ROLE.isEmpty()) {
            for (User2Role u2r : SELF_ROLE) {
                if (u2r.getRoleId().longValue() == roledid.longValue()) {
                    return "checked=\"checked\"";
                }
            }
        }
        return "";
    }

    /** 保存用户对象 */
    @Override
    public String execute(){
        String flag = "a";
        if (user.getId() != null) {
            flag = "u";
        }
        userBo.save(user, myRole);
        SELF_ROLE = Lists.newArrayList();
        return ajax(flag + user.getId());
    }

    // 分配角色
    public String signtRoll(){
        userBo.saveRole2User(user.getId(), myRole);
        return ajax(user.getId());
    }

    public User getUser(){
        return user;
    }

    public void setUser(User user){
        this.user = user;
    }

    public void setUserBo(UserBo userBo){
        this.userBo = userBo;
    }

    public String getRolelist(){
        return rolelist;
    }

    public void setRoleBo(RoleBo roleBo){
        this.roleBo = roleBo;
    }

    public void setMyRole(Long[] myRole){
        this.myRole = myRole;
    }

    public void setOripwd(String oripwd){
        this.oripwd = oripwd;
    }
}
